package com.wnxy.controller;


import com.wnxy.entity.CartPreviewVo;
import com.wnxy.entity.CartVo;
import com.wnxy.entity.User;
import com.wnxy.service.CartService;
import com.wnxy.service.UserService;
import com.wnxy.utils.UserTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 林夕
 * @since 2023-03-11 04:45:21
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    UserService userService;

    @Autowired
    CartService cartService;

    private User getUser(HttpServletRequest request){
        String username = UserTokenUtil.getUserAccount(request);
        User user = userService.getUserByName(username);
        return user;
    }

    /* 获取订单预览页信息 */
    @RequestMapping("/preview")
    public CartPreviewVo preview(@RequestParam("weaponIds[]") Integer[] weaponIds, HttpServletRequest request){
        //获取购物车信息
        User user = getUser(request);
        List<CartVo> cartVoList = cartService.getCartVoPreview(user.getId(), weaponIds);
        //获取总价
        BigDecimal totalPrice = cartService.totalPrice(user.getId(), weaponIds);
        CartPreviewVo cartPreviewVo = new CartPreviewVo();
        cartPreviewVo.setCartVoList(cartVoList);
        cartPreviewVo.setTotalPrice(totalPrice);
        return cartPreviewVo;
    }

}

